<?php
namespace Home\Controller;

class EditpwdController extends CommonController {

	
    //加载修改密码视图
    public function update(){
    	$this->display('Login:editpass');
    }

    //执行修改密码
    public function edit(){
    	$data = I('post.');
    	$pass = D('user');
    	
    	//新密码
    	$newpass['pass'] = md5($data['newpass']);
    	$id = session('user')['id'];
    	
    	//触发自动验证
    	if(!$res = $pass->create($data)){
    		$this->error($pass->getError());
    		//判断原密码是否正确
    	}elseif(md5($data['oldpass']) == session('user')['pass']){
    		//判断密码是否修改成功
    		if($pass->where('id='.$id)->save($newpass)){
    			session('user',null);
    			$this->success('修改成功,请重新登录',U('Login/index'));
    		}else{
    			$this->error('修改失败');
    		}
    	}else{
    		$this->error('原密码不正确');
    	}
    }
}